<template>
  <div>
    <div>
      <el-form inline>
        <el-form-item label="日期选择" style="width: 300px;">
          <el-date-picker @clear="getTableList" clearable type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD" placeholder="请选择创建时间" v-model="searchFrom.createdAt"
            @change="dateChange" />
        </el-form-item>
        <el-form-item label="控制方式" style="width:220px">
          <el-select @clear="getTableList" clearable clear-table v-model="searchFrom.operatePlatform"
            placeholder="请选择控制方式">
            <el-option :label="item.label" :value="item.value" v-for="item in CONTROLMODE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作状态" style="width:220px">
          <el-select @clear="getTableList" clearable clear-table v-model="searchFrom.result"
            placeholder="请选择操作状态">
            <el-option :label="item.label" :value="item.value" v-for="item in CONTROLRESULT"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="DebounceGetTableList">搜索</el-button>
          <sj-button custom-type="sinfo" @click="DebounceResetting">重置</sj-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button type="danger" :icon="Delete" :disabled="!selectionData.length" @click="deletes()">批量删除</el-button>
    </div>
    <div>
      <pure-table class="mt-5" :data="tableData.data" :columns="initPureTableConfig(roleTableConfig)" showOverflowTooltip border
        @selection-change="selectionChange" :isShowPag="false">
        <template #operatePlatform="{ row }">
          <span>{{ isNullOrEmpty( CONTROLTYPETEXT[row.operatePlatform]?.label ) }}</span>
        </template>
        <template #operateResult="{ row }">
          <el-tag :type="row.operateResult == 1? 'success' : 'danger'">
            <span>{{ isNullOrEmpty( CONTROLRESULTTEXT[row.operateResult]?.label ) }}</span>
        </el-tag>
         
        </template>
        <template #action="{ row }">
          <el-space>
            <sj-link custom-type="sblue" :underline="false" @click="edit(row)">编辑</sj-link>
            <sj-link custom-type="sred" :underline="false" @click="deletes(row)">删除</sj-link>
          </el-space>
        </template>
      </pure-table>
      <div class="mt-5">
        <el-pagination v-model:current-page="searchFrom.page" v-model:page-size="searchFrom.limit" background
          :layout="PAGE_LAYOUT" :total="tableData.total" @size-change="getTableList" @current-change="getTableList" />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {isNullOrEmpty,initPureTableConfig} from '@/utils/functions'
import { PAGE_LAYOUT } from '@/constant';
import { CONTROLMODE } from "@/constant/device-mangenment";
import { ref, reactive } from "vue";
import { NOTIFICATIONSTATUS, CONTROLRESULTTEXT, CONTROLTYPETEXT, CONTROLRESULT } from "@/constant/alarm-mangenment";
import {
  cloneDeep,
  isAllEmpty,
  deviceDetection,
  debounce
} from "@pureadmin/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import { getList, deleteInfo } from "@/api/operate/device-management/control-records";
const tableData = reactive({
  data: [],
  total: 0
});
const props = defineProps({
  deviceId: {
    type: String,
    default: ''
  }
})
const searchFrom = reactive({
  page: 1,
  limit: 10,
  operatePlatform: "",
  createdAt: [],
  startDate: "",
  endDate: ""
});
const selectionData = ref([]);
const selectionChange = data => {
  selectionData.value = data;
};
const dateChange = e => {
  if (e) {
    searchFrom.startDate = e[0];
    searchFrom.endDate = e[1];
  } else {
    searchFrom.startDate = "",
      searchFrom.endDate = "",
      getTableList()
  }
};
const roleTableConfig = [
  { type: "selection", align: "left" },
  { prop: "id", label: '编号', width: 100 },
  { prop: "operateContent", label: "操作内容" , minWidth: 150},
  { prop: "operateIP", label: "操作IP" , minWidth: 150},
  { slot: "operatePlatform", label: "控制方式" , minWidth: 150},
  { slot: "operateResult", label: "操作状态" , minWidth: 150},
  { prop: "operateParam", label: "操作参数", minWidth: 120 },
  { prop: "operateResultContent", label: "操作结果", minWidth: 120 },
  { prop: "operator", label: "操作人" , minWidth: 150},
  { prop: "createdAt", label: "操作时间" , minWidth: 160}
  // { slot: "action", label: "操作", width: 150, fixed: "right" }
];
const defaultSearchForm = () => {
  return {
    operatePlatform: "",
    createdAt: [],
    startDate: "",
    endDate: "",
    result: "",
		page: 1,
    limit: 10,
  }
}
// 重置搜索栏
const resetting = () => {
  //点击重置按键
  Object.assign(searchFrom, defaultSearchForm());
  getTableList();
};
// 获取列表数据
const getTableList = () => {
  getList({ ...searchFrom, deviceId: props.deviceId }).then(res => {
    if (res.code === 200) {
      tableData.data = res.result.data;
      tableData.total = res.result.total;
    }
  });
};
// 删除
const deletes = (row?) => {
  ElMessageBox.confirm(`确认删除${row ? "当前" : "选中的"}数据?`, "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteInfo({
      ids: row ? [row.id] : selectionData.value.map(item => item.id)
    }).then(res => {
      if (res.code == 200) {
        ElMessage({
          type: "success",
          message: "删除成功"
        });
        getTableList();
      }
    });
  });
};
getTableList()
const DebounceResetting = debounce(resetting, 1000, true);

const DebounceGetTableList = debounce(getTableList, 1000, true);
</script>
